package controller

import (
	"fx_swift_ifcgate/api"
	"fx_swift_ifcgate/internal/service"
	"fx_swift_ifcgate/pkg/code"
	"fx_swift_ifcgate/pkg/internalerrors"
	"github.com/gin-gonic/gin"
	"net/http"
)

func GetCountryInfo(c *gin.Context) {
	result, err := service.GetCountryInfo(c)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

func GetCurrencyInfo(c *gin.Context) {
	result, err := service.GetCurrencyInfo(c)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

func PostTp6001(c *gin.Context) {
	req := &api.Tp6001Req{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	result, err := service.PostTp6001(req, "")
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

func PostTp6002(c *gin.Context) {
	req := &api.Tp6002Req{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	result, err := service.PostTp6002(req, "")
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

func PostTp6003(c *gin.Context) {
	req := &api.Tp6003Req{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	result, err := service.PostTp6003(req, "")
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

func PostTp6004(c *gin.Context) {
	req := &api.Tp6004Req{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	result, err := service.PostTp6004(req, "")
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 收款人分类
func GetPayeeType(c *gin.Context) {
	typeCode := c.Query("typeCode")
	result, err := service.GetPayeeType(c, typeCode)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 汇款用途
func GetTradePurpose(c *gin.Context) {
	result, err := service.GetTradePurpose(c)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 付款目的
func GetPurposeList(c *gin.Context) {
	result, err := service.GetPurposeList(c)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 检查邀请码是否正确
func CheckInvitationCode(c *gin.Context) {
	invitationCode := c.Query("invitation_code")
	result, err := service.CheckInvitationCode(c, invitationCode)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}
